<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <include file="Public:resource"/>
    <title>预存清单详情</title>
</head>
<body>
<div class="container">
    <include file="Public:top" title="预存充值"/>

    <eq name="json.useratr" value="2">
        <!--预存账户-->
        <div class="row" style="margin-top: 20px;font-size: medium">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <div class="row bd-bot">
                    <div class="col-xs-5 col-sm-5 col-md-5 col-lg-5" align="center">
                        水费账号
                    </div>
                    <div class="col-xs-7 col-sm-7 col-md-7 col-lg-7 c-gray" >
                        <span id="userid">{$json.userid}</span>
                    </div>
                </div>
                <div class="row bd-bot">
                    <div class="col-xs-5 col-sm-5 col-md-5 col-lg-5" align="center">
                        用户姓名
                    </div>
                    <div class="col-xs-7 col-sm-7 col-md-7 col-lg-7 c-gray">
                        {$json.name}
                    </div>
                </div>
                <div class="row bd-bot">
                    <div class="col-xs-5 col-sm-5 col-md-5 col-lg-5" align="center">
                        用户地址
                    </div>
                    <div class="col-xs-7 col-sm-7 col-md-7 col-lg-7 c-gray">
                        {$json.address}
                        元
                    </div>
                </div>
                <div class="row bd-bot">
                    <div class="col-xs-5 col-sm-5 col-md-5 col-lg-5" align="center">
                        账号余额
                    </div>
                    <div class="col-xs-7 col-sm-7 col-md-7 col-lg-7 c-red">
                        <input type="hidden" value="{$json.balance}" id="balance"><!--分-->
                        <input type="hidden" value="0" id="arrears"><!--分-->
                        <input type="hidden" value="{$minSave}" id="minSave"><!--分-->
                        ￥
                        <span class="c-red">
                            <script>
                                var balance = parseInt('{$json.balance}');
                                var arrears = parseInt('{$json.arrears}');
                                if(balance > 0){
                                    document.write(FenToYuan(balance));
                                } else {
                                    document.write(FenToYuan(arrears));
                                    if(arrears != 0){
                                        $("#arrears").val(arrears);
                                    }
                                }

                            </script>
                        </span>
                        元
                    </div>
                </div>
                <!--<div class="row bd-bot">
                    <div class="col-xs-5 col-sm-5 col-md-5 col-lg-5" align="center">
                        欠费金额
                    </div>
                    <div class="col-xs-7 col-sm-7 col-md-7 col-lg-7 c-red">
                        <span class="c-red">￥
                            <script>
                                document.write(FenToYuan('{$json.arrears}'));
                            </script>
                        </span>
                        元
                    </div>
                </div>-->
            </div>
        </div>

        <include file="Public:bar" text="充值"/>

        <div class="row">
            <div class="col-xs-12 col-sm-12 col-mg-12 col-md-12" align="center">
                <input type="number" placeholder="请输入充值金额(单位:元)" name="money" id="money" style="width: inherit;"
                       align="center">
                <span class="c-gray" style="position: relative;;float: right;top:-27px;left:-27px;">元</span>
            </div>
        </div>

        <div class="row" style="margin-top: 20px;">
            <div class="col-xs-12 col-sm-12 col-mg-12 col-md-12">
                <div>
                    <input type="button" class="btn btn-lg btn-block"
                           style="background-color:#6bd1a2;color:white;height: 46px;"
                           onclick="onPay()" value="微信支付"/>
                </div>
            </div>
        </div>


        <else/>
        <!--非预存账户-->

        <div class="row" style="margin-top: 50px;">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" style="font-size: large;" align="center">
                该账户不是预存账户,不能查询预存余额
            </div>
        </div>
    </eq>


</div>
<include file="Public:progress" hint="请等待..."/>
<include file="Public:message"/>
<script>
    //点击微信支付
    function onPay() {
        var inputMoney = $("#money").val().trim();
        //验证输入格式
        var pattern = /^[0-9]+(.[0-9]{1,2})?$/;
        var moneyInYuan = parseFloat(inputMoney);
        if ((!pattern.test(inputMoney)) || moneyInYuan == 0.0) {
            alert("输入金额非法(必须大于0且最多两位小数)!");
            return;
        }
        var totalFee = parseInt(parseFloat(moneyInYuan * 100)); //分钱
        //验证金额是否符合要求
        var conditionFee = 0;//充值最低金额
        var minSave = parseInt($("#minSave").val());//规定最低充值金额
        var arrears = parseInt($("#arrears").val());//欠费金额
        if(arrears < 0){
            var positiveArrears = Math.abs(arrears);
            conditionFee = minSave > positiveArrears ? minSave : positiveArrears;
        } else{
            conditionFee = minSave;
        }
        //alert("ConditionFee:" + conditionFee);
        //alert(totalFee+":" + conditionFee);
        if (totalFee < conditionFee) {
            var positiveArrearsInYuan = FenToYuan(Math.abs(arrears));
            alert("充值金额必须大于"+(parseFloat(conditionFee)/100).toFixed(2)+"元!");
            return;
        }
        //验证通过，下单
        var userid = $("#userid").text();
		//alert(userid);
        var param = {
            totalFee: totalFee,
            userid: userid
        }
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: param,
            async: false,
            url: "{:U('PreSave/placeOrderInWeiXin')}",
            contentType: "application/x-www-form-urlencoded;charset=utf-8",
            success: function (json) {
                if (json.status == 'success') {
                    pay(json);
                } else {
                    alert(json.msg);
                    window.location.reload();
                }
            },
            error: function (msg) {
                alert("微信下单失败,请检查网络!");
            }
        });
    }

    /**
     * 支付方法
     */
    function pay(json) {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        } else {
            onBridgeReady(json);
        }
    }

    function onBridgeReady(json) {
        WeixinJSBridge.invoke(
                'getBrandWCPayRequest', json.jsApiParameters,
                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        //todo 调用缴费接口
                        ReCharge(
                                json.orderInfo.userId,
                                json.orderInfo.userAtr,
                                json.orderInfo.tradeNo,
                                json.orderInfo.way,
                                json.orderInfo.payType,
                                json.orderInfo.payTime,
                                json.orderInfo.amount
                        );
                    }
                }
        );
    }

    function ReCharge(userId, userAtr, tradeNo, way, payType, payTime, amount) {
        var param = {
            userId: userId,
            userAtr: userAtr,
            tradeNo: tradeNo,
            way: way,
            payType: payType,
            payTime: payTime,
            amount: amount
        }
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: param,
            async: false,
            url: "{:U('PreSave/Recharge')}",
            contentType: "application/x-www-form-urlencoded;charset=utf-8",
            success: function (json) {
                if (json.status == "success") {
                    var name = '{$json.name}';
                    var address = '{$json.address}';
                    var charge = parseFloat($("#money").val()).toFixed(2);//元
                    var lastBalance = 0 ;//充值前余额，balance>0取balance，否则取arrears
                    var vBalance = parseInt($("#balance").val());
                    var vArrears = parseInt($("#arrears").val());
                    if(vBalance > 0){
                        lastBalance = FenToYuan(vBalance);
                    } else{
                        lastBalance = FenToYuan(vArrears);
                    }
                    successParam = {
                        userId: userId,
                        payTime: payTime,
                        amount: amount,
                        name: name,
                        address: address,
                        lastBalance: lastBalance,
                        charge: charge
                    };

                    window.location.href = "{:U('PreSave/success')}&param=" + JSON.stringify(successParam);
                } else {
                    alert(json.msg);
                }
            },
            error: function (msg) {
                alert('请求失败!');
            }
        })

    }
</script>
</body>
</html>